"""
Case Type   : datakit安装
Case Name   : 【服务器管理】搜索物理机
Create At   : 2025/5/20
Owner       : @lonely-dance
Description :
    1.添加服务器
    2.使用存在的IP搜索服务器
    3.使用已设置的标签搜索服务器
    4.使用存在的操作系统搜索服务器
    5.查询条件为空搜索服务器
    6.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
History     :
"""

import os
import re
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.datakit_api.CommonApi import CommonApi
from testcase.utils.CommonDatakit import CommonDatakit


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.pri_root_node = Node('PrimaryRoot')
        self.pri_node = Node('PrimaryDbUser')
        self.datakit_node = Node('Datakit_Root')
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.log.info(self.token)
        self.assertNotEqual("error", self.token)
        self.com_api = CommonApi(self.token)
        self.com_datakit = CommonDatakit(self.token)
        self.tag_id = ''

    def test_base_ops(self):
        text = f'----step1:添加服务器 expect：成功----'
        self.log.info(text)
        data = {"hostId": "",
                "isRemember": True,
                "name": f"{self.pri_node.ssh_host}",
                "password": f"{self.pri_node.ssh_password}",
                "port": 22,
                "privateIp": f"{self.pri_node.ssh_host}",
                "publicIp": f"{self.pri_node.ssh_host}",
                "remark": "",
                "tags": ["test"],
                "username": f"{self.pri_node.ssh_user}"}
        response = self.com_api.post_host(data)
        self.log.info(response.json())

        text = '----step2:使用存在的IP搜索服务器 expect:成功----'
        self.log.info(text)
        response = self.com_api.get_host_page(name=f"{self.pri_root_node.ssh_host}")
        self.log.info(response.json())
        self.assertEqual(1, response.json()['total'], '执行失败' + text)

        text = '----step3:使用已设置的标签搜索服务器 expect:成功----'
        self.log.info(text)
        response = self.com_api.get_hostTag_page(name='test')
        self.log.info(response.json())
        self.tag_id = response.json()['rows'][0]['id']
        response = self.com_api.get_host_page(tag_ids=self.tag_id)
        self.log.info(response.json())
        self.assertEqual(1, response.json()['total'], '执行失败' + text)

        text = '----step4:使用存在的操作系统搜索服务器 expect:成功'
        self.log.info(text)
        cmd = 'cat /etc/system-release;'
        res = self.pri_root_node.sh(cmd).result().strip().split()[0].lower()
        response = self.com_api.get_host_page(system_os=res)
        self.log.info(response.json())
        self.assertEqual(1, response.json()['total'], '执行失败' + text)

        text = '----step5:查询条件为空搜索服务器 expect:成功'
        self.log.info(text)
        response = self.com_api.get_host_page()
        self.log.info(response.json())
        self.assertEqual(1, response.json()['total'], '执行失败' + text)

        text = '----step6:删除标签 expect:成功'
        self.log.info(text)
        response = self.com_api.delete_hostTag_del(self.tag_id)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)

    def tearDown(self):
        self.log.info('----step6:清理环境 expect:成功----')
        response = self.com_api.delete_hostTag_del(self.tag_id)
        self.log.info(response.json())

        self.com_datakit.delete_host()

        self.assertEqual(200, response.json()['code'], '执行失败')
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
